
import { _decorator, Component, Node, game } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import GlobalPool, { customPoolScript } from '../../Script/Common/GlobalPool';
import Loader from '../../Script/Common/Loader';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import GlobalData from '../../Script/Common/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('TryMaxCarUI')
export class TryMaxCarUI extends yyComponent {

    public show() {
        this.node.active = true;
        this.showCar();
    }
    public hide() {
        this.node.active = false;
        GlobalPool.putAllChildren(this.carStage);
    }

    @property(Node)
    protected carStage: Node = null;
    protected showCar() {
        let str = "ShopCar3_3";
        let node = GlobalPool.get(str);
        if (!!node) {
            this.addCar(node);
        } else {
            Loader.loadBundle("Car", () => {
                Loader.loadBundleRes("Car", "Prefab/ui/" + str, (res) => {
                    if (!res) {
                        return;
                    }
                    GlobalPool.createPool(str, res, "ShopCar");
                    let node2 = GlobalPool.get(str);
                    this.addCar(node2);
                });
            });
        }
    }
    protected addCar(node: Node) {
        GlobalPool.putAllChildren(this.carStage);
        this.carStage.addChild(node);
        node[customPoolScript].show();
    }

    protected onBtnCancel() {
        this.playClickBtn();
        this.hideUI(GlobalEnum.UI.tryMaxCar);
    }

    protected onBtnOk() {
        this.playClickBtn();
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: () => {
                GlobalData.set(GlobalEnum.myGlobalDataType.tryMaxCar, true);
                this.emit(EventType.Common.DirectorEvent.enterLevel);
            },
        });
    }

}
